package com.khaled.protclass.model.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;

public class UtilDirectory
{
	public static void main(String[] args)
	{
		String directory = "C:\\Computer\\Research\\PhD\\BacterialVirulentPrediction\\VirulentPred - Dataset\\Independent-data\\Independent-II\\output\\BLAST\\CD\\output";
		String[] files = getDirectoryChildren(directory, ".blast");
		for (String filePath : files)
		{
			File file = new File(directory + "\\" + filePath);
			File file2 = new File(directory + "\\" + "I" + filePath);
			boolean success = file.renameTo(file2);
			System.out.println(success);

		}
	}

	public static boolean exist(String path)
	{
		File f = new File(path);
		return f.exists();
	}

	public static boolean isFile(String path)
	{
		File f = new File(path);
		return f.isFile();
	}

	public static String[] getDirectoryChildren(String path, String ext)
	{
		File dir = new File(path);
		ExtensionFilter filter = new ExtensionFilter(ext);
		String[] children = dir.list(filter);
		return children;
	}

	public static ArrayList<String> readFileLines(String filePath) throws IOException
	{
		FileReader input = null;
		BufferedReader bufRead = null;
		ArrayList<String> lines = new ArrayList<String>();

		input = new FileReader(filePath);
		bufRead = new BufferedReader(input);
		String line;
		line = bufRead.readLine();
		while (line != null)
		{
			lines.add(line.trim());
			line = bufRead.readLine();
		}
		bufRead.close();
		input.close();
		return lines;
	}

	public static ArrayList<String> readFileLines(File file) throws IOException
	{
		FileReader input = null;
		BufferedReader bufRead = null;
		ArrayList<String> lines = new ArrayList<String>();

		input = new FileReader(file);
		bufRead = new BufferedReader(input);
		String line;
		line = bufRead.readLine();
		while (line != null)
		{
			lines.add(line.trim());
			line = bufRead.readLine();
		}
		bufRead.close();
		input.close();
		return lines;
	}

	public static String readFileToEnd(String fastaFilePath) throws IOException
	{
		StringBuffer content = new StringBuffer();
		FileReader input = null;
		BufferedReader bufRead = null;
		input = new FileReader(fastaFilePath);
		bufRead = new BufferedReader(input);

		String line;
		line = bufRead.readLine();
		while (line != null)
		{
			content.append(line.trim());
			line = bufRead.readLine();
		}
		bufRead.close();
		input.close();
		return content.toString();
	}

	public static String createDirectory(String path, String name)
	{
		String directoryPath = path + "\\" + name;

		(new File(directoryPath)).mkdir();

		return directoryPath;
	}

	public static void createFile(String content, String filePath)
	{
		try
		{
			FileWriter fstream = new FileWriter(filePath);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(content);
			out.close();
		}
		catch (IOException ex)
		{
			System.out.println(ex.getMessage());
		}
	}

	public static void deleteFiles(String directory, String ext)
	{
		String[] files = getDirectoryChildren(directory, ext);
		for (String f : files)
		{
			new File(directory + "//" + f).delete();
		}
	}

	public static void deleteFile(String filePath)
	{
		new File(filePath).delete();
	}
}
class ExtensionFilter implements FilenameFilter
{

	private String extension;

	public ExtensionFilter(String extension)
	{
		this.extension = extension;
	}

	public boolean accept(File dir, String name)
	{
		return (name.endsWith(extension));
	}

}

